import { UserListAlltype } from '@/api/department/types'
import { BaseButton } from '@/components/Button'
import { DescriptionsSchema } from '@/components/Descriptions'
import { FormSchema } from '@/components/Form'
import { useDictStore } from '@/store/modules/dictData'
import { usePatrolGroupStore } from '@/store/modules/PlanGroupStore'
import { useUserStore } from '@/store/modules/user'
import { ElMessageBox, ElImage } from 'element-plus'
import { computed, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { PlanEventTrack } from '@/api/Emergency/type'

const GroupStore = usePatrolGroupStore()
const { required } = useValidator()

GroupStore.setUserListAll() //获取执行用户列表
const DictStore = useDictStore()
const userStore = useUserStore()
DictStore.setDict('tb_track_way') //跟踪方式字典
DictStore.setDict('pian_qat_type') //跟踪方式字典
DictStore.setDict('solve_choice') //处置选择字典tb_track_way
//执行用户列表
const UserList = computed(() => {
  return GroupStore.getUserListAll
})
export function FormHooks() {
  const trackFile: any = ref([])
  const rules = reactive({
    trackUser: [required()],
    trackWay: [required()],
    trackDate: [required()]
  })
  // 新增跟踪表单信息
  const FormSchemas = reactive<FormSchema[]>([
    {
      field: 'eventUuid',
      label: '台账uuid',
      component: 'Input',
      hidden: true,
      componentProps: {
        disabled: true
      }
    },
    {
      field: 'trackUser',
      label: '跟踪人员',
      component: 'Select',
      componentProps: {
        options: UserList.value.map((item: UserListAlltype) => {
          return {
            label: item.userName,
            value: item.userName
          }
        })
      },
      colProps: {
        span: 18
      }
    },

    {
      field: 'trackWay',
      label: '跟踪方式',
      component: 'RadioGroup',
      colProps: {
        span: 20
      },
      componentProps: {
        options: DictStore.$state.tb_track_way?.map((item) => {
          return {
            label: item.dictLabel,
            value: item.dictValue
          }
        })
      }
    },

    {
      field: 'trackDate',
      label: '跟踪时间',
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      },
      colProps: {
        span: 18
      }
    },
    {
      field: 'trackSituation',
      label: '跟踪情况',
      component: 'Input',
      colProps: {
        span: 20
      }
    },
    {
      field: 'trackFile',
      component: 'Upload',
      label: '跟踪附件',
      componentProps: {
        accept: 'image/*',
        action: '/api/file/upload',
        multiple: true,
        listType: 'picture',
        headers: { Authorization: userStore.getToken },
        beforeRemove: (uploadFile: any) => {
          return ElMessageBox.confirm(`确定要移除 ${uploadFile.name} ?`).then(
            () => true,
            () => false
          )
        },
        onSuccess: (file: any) => {
          trackFile.value.push(file.data.url)
          return file.data
        },
        slots: {
          default: () => <BaseButton type="primary">上传</BaseButton>,
          tip: () => <div class="el-upload__tip">请上传小于500KB的jpg/png</div>
        }
      },
      colProps: {
        span: 20
      }
    }
  ])
  const saveLoading = ref(false)
  const dialogVisible = ref(false)
  const TrackFormData = ref<PlanEventTrack>({}) //跟踪表单数据
  return { trackFile, FormSchemas, rules, saveLoading, dialogVisible, TrackFormData }
}
const PATH_URL = import.meta.env.VITE_API_PHOTO_PATH
console.log(PATH_URL,import.meta.env.VITE_API_PHOTO_PATH);
export function DetailHooksSchema() {
  const DistributeDetailSchema = reactive<DescriptionsSchema[]>([
    {
      field: 'createTime',
      label: '指派时间'
    },
    {
      field: 'assignUser',
      label: '指派用户'
    },
    {
      field: 'description',
      label: '处置要求'
    },
    {
      field: 'endDate',
      label: '截止时间'
    },
    {
      field: 'qatType',
      label: '验收类型',
      slots: {
        default: (data: any) => {
          return DictStore.$state.pian_qat_type?.map((item) => {
            if (item.dictValue == data.hazardType) {
              return item.dictLabel
            }
          })
        }
      }
    },
    {
      field: 'qatUser',
      label: '验收人员'
    }
  ])
  const SolveDetailSchema = reactive<DescriptionsSchema[]>([
    {
      field: 'solveUser',
      label: '处置人'
    },
    {
      field: 'solveCircs',
      label: '处置情况',
      slots: {
        default: (data: any) => {
          if (data.solveChoice == '0') {
            return '部分完成'
          } else {
            return '全部完成'
          }
        }
      }
    },
    {
      field: 'isCorrect',
      label: '是否整改',
      slots: {
        default: (data: any) => {
          if (data.isCorrect == '0') {
            return '否'
          } else {
            return '是'
          }
        }
      }
    },
    {
      field: 'collaborator',
      label: '协同人员'
    },
    {
      field: 'solveChoice',
      label: '处置选择',
      slots: {
        default: (data: any) => {
          return DictStore.$state.solve_choice?.map((item) => {
            if (item.dictValue == data.solveChoice) {
              return item.dictLabel
            }
          })
        }
      }
    },
    {
      field: 'solveDes',
      label: '处置描述'
    },
    {
      field: 'isSave',
      label: '是否暂存',
      slots: {
        default: (data: any) => {
          if (data.isSave == '0') {
            return '缓存'
          } else {
            return '提交'
          }
        }
      }
    },
    {
      field: 'qatUser',
      label: '验收人员'
    },
    {
      field: 'auditStatus',
      label: '是否通过',
      slots: {
        default: (data: any) => {
          if (data.auditStatus == '0') {
            return '不通过'
          } else {
            return '通过'
          }
        }
      }
    },
    {
      field: 'reason',
      label: '原因'
    }
  ])
  const TrackDetailSchema = reactive<DescriptionsSchema[]>([
    {
      field: 'trackUser',
      label: '跟踪人员'
    },
    {
      field: 'trackDate',
      label: '跟踪时间'
    },
    {
      field: 'trackWay',
      label: '跟踪方式',
      slots: {
        default: (data: any) => {
          return DictStore.$state.tb_track_way?.map((item) => {
            if (item.dictValue == data.trackWay) {
              return item.dictLabel
            }
          })
        }
      }
    },
    {
      field: 'trackSituation',
      label: '跟踪情况'
    },
    {
      field: 'createBy',
      label: '创建人'
    },
    {
      field: 'createTime',
      label: '创建时间'
    },
    {
      field: 'trackFile',
      label: '跟踪附件',
      slots: {
        default: (data: any) => {
          const res = JSON.parse(data.trackFile)
          return res.map((item) => {
            console.log(PATH_URL + item);
            return (
              <ElImage
                style="width: 70px; height: 70px;margin-left:10px"
                src={PATH_URL + item}
                preview-src-list={res}
                fit="cover"
              />
            )
          })
        }
      }
    }
  ])
  return { DistributeDetailSchema, SolveDetailSchema, TrackDetailSchema }
}
